﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WeiXinWeb.Services;
using WeiXinWeb.Models;
using System.Data;
using System.Text;

namespace WeiXinWeb.Server
{
    public class TruckServer : PSIBaseService
    {
        public bool VerifyLogin(TruckCompany model)
        {
            var sql = "select count(*) from tms_truck_company where n_tel=@tel and v_password=@password";
            var p =CreateDbParameters();
            p.AddWithValue("tel", model.tel);
            p.AddWithValue("password", model.password);

            var flag = QueryCount(sql, p);
            if (flag > 0)
                return true;
            else
                return false;
        }

        public TruckCompany GetSingleObject(TruckCompany model)
        {
            var sql =new StringBuilder("select * from tms_truck_company where 1=1");
            var p=CreateDbParameters();
            if (!string.IsNullOrEmpty(model.Id))
            {
                sql.Append(" and id=@id");
                p.AddWithValue("id", model.Id);
            }
            if(!string.IsNullOrEmpty(model.Name))
            {
                sql.Append(" and f_name=@name");
                p.AddWithValue("name", model.Name);
            }
            if (!string.IsNullOrEmpty(model.tel))
            {
                sql.Append(" and n_tel=@tel");
                p.AddWithValue("tel", model.tel);
            }
           var result=QueryObject<TruckCompany>(sql.ToString(), p,
               reader =>
               {
                   return new TruckCompany
                   {
                       Id = reader.GetString(0),
                       Name = reader.GetString(1),
                       owner=reader.GetString(3),
                       tel =reader.GetString(4),
                       password=reader.GetString(5)
                      
                   };
            });
           return result;
        
        }


        public bool UpdatePW(TruckCompany model)
        {
            var sql = new StringBuilder("update tms_truck_company set v_password=@pd where id=@id");
            var p = CreateDbParameters();
            p.AddWithValue("pd", model.password);
            p.AddWithValue("id", model.Id);
            var result = Execute(sql.ToString(), p);
            if (result > 0)
                return true;
            else
                return false;
        }


        public List<Truck> Getcarlist(string id)
        {
            var result = new List<Truck>();
            var sql = new StringBuilder("select * from tms_truck where parent_id=@id");
            var p = CreateDbParameters();
            p.AddWithValue("id", id);
            Query(sql.ToString(),p,reader=>{
               var temp=new Truck{
                   Id=reader.GetString(reader.GetOrdinal("id")),
                   ParentId = reader.GetString(reader.GetOrdinal("parent_id")),
                   TruckNumber = reader.GetString(reader.GetOrdinal("f_truck_num")),
                   Driver = reader.GetString(reader.GetOrdinal("f_driver")),
                   DriverTel = reader.GetString(reader.GetOrdinal("f_driver_tel"))
               };
               result.Add(temp);
            });
            return result;
        }


       
    }
}